package com.ifreshwing.view.camera;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.PointF;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewTreeObserver;
import android.widget.ImageView;


public class TakePictureBorderView201 extends ImageView {
    /**
     * 水平方向与View的边距
     */
    private int mHorizontalPadding;
    /**
     * 垂直方向与View的边距
     */
    private int mVerticalPadding;

    private int nWidth;
    private int nHeight;

    private int mWidth = 0;
    private int mHeight = 0;

    private int oWidth;
    private int oHeight;
    private int olpx;
    private int otpx;


    private Matrix mMatrix = new Matrix();
    private float mScaleFactor = 1.0f;
    private float mRotationDegrees = 0.f;
    private float mFocusX = 0.f;
    private float mFocusY = 0.f;
    private int mAlpha = 255;
    private int mImageHeight, mImageWidth;
    private boolean once = true;

    /**
     * 边框的颜色，默认为白色
     */
    private int mBorderColor = Color.parseColor("#FFFFFF");
    /**
     * 边框的宽度 单位dp
     */
    private int mBorderWidth = 1;

    private Paint mPaint;

    private Bitmap mask = null;
    private Bitmap maskIn = null;
    private MergePhotoItem mergePhotoItem;

    public TakePictureBorderView201(Context context) {
        this(context, null);
    }

    public TakePictureBorderView201(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TakePictureBorderView201(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        mBorderWidth = (int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, mBorderWidth, getResources()
                        .getDisplayMetrics());
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
//        mPaint.setColor(Color.parseColor("#aa000000"));
        mPaint.setAlpha(0);
        mPaint.setStyle(Style.FILL);

        float ws = Float.parseFloat("1040") / mWidth;
        float hs = Float.parseFloat("1440") / mHeight;
        if (ws >= hs) {
            nWidth = mWidth;
            nHeight = (int) (Integer.parseInt("1440") / ws + 0.5);
            mHorizontalPadding = 0;
            mVerticalPadding = (mHeight - nHeight) / 2;
            oWidth = (int) (Integer.parseInt("1040") / ws + 0.5);
            oHeight = (int) (Integer.parseInt("1440") / ws + 0.5);
            olpx = (int) (Integer.parseInt("0") / ws + 0.5);
            otpx = (int) (Integer.parseInt("0") / ws + 0.5) + mVerticalPadding;
        } else {
            nWidth = (int) (Integer.parseInt("1040") / hs + 0.5);
            nHeight = mHeight;
            mHorizontalPadding = (mWidth - nWidth) / 2;
            mVerticalPadding = 0;
            oWidth = (int) (Integer.parseInt("1040") / hs + 0.5);
            oHeight = (int) (Integer.parseInt("1440") / hs + 0.5);
            olpx = (int) (Integer.parseInt("0") / hs + 0.5) + mHorizontalPadding;
            otpx = (int) (Integer.parseInt("0") / hs + 0.5);
        }

        // 绘制左边1
        canvas.drawRect(0, 0, mHorizontalPadding, getHeight(), mPaint);
        // 绘制右边2
        canvas.drawRect(getWidth() - mHorizontalPadding, 0, getWidth(),
                getHeight(), mPaint);
        // 绘制上边3
        canvas.drawRect(mHorizontalPadding, 0, getWidth() - mHorizontalPadding,
                mVerticalPadding, mPaint);
        // 绘制下边4
        canvas.drawRect(mHorizontalPadding, getHeight() - mVerticalPadding,
                getWidth() - mHorizontalPadding, getHeight(), mPaint);
        mask = ImageUtil.getImageFromAssetsFileResize(getContext(), "local2031-1.png", nWidth, nHeight);
        if (mask != null) {
            if (maskIn != null) {
                Rect dest = new Rect(0, 0, getResources().getDisplayMetrics().widthPixels, getResources().getDisplayMetrics().heightPixels);
                canvas.drawBitmap(maskIn, null, dest, null);
            }
            Rect dest = new Rect(mHorizontalPadding, mVerticalPadding, getWidth()
                    - mHorizontalPadding, getHeight() - mVerticalPadding);
            canvas.drawBitmap(mask, null, dest, null);
        } else {
            // 绘制外边框
//            mPaint.setColor(mBorderColor);
            mPaint.setStrokeWidth(mBorderWidth);
            mPaint.setStyle(Style.STROKE);
            canvas.drawRect(mHorizontalPadding, mVerticalPadding, getWidth()
                    - mHorizontalPadding, getHeight() - mVerticalPadding, mPaint);
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        mWidth = getWidth();
        mHeight = getHeight();
    }

    public MergePhotoItem getMergePhotoItem() {
        return mergePhotoItem;
    }

    public void setMergePhotoItem(MergePhotoItem mergePhotoItem) {
        this.mergePhotoItem = mergePhotoItem;
    }

    public int getmHorizontalPadding() {
        return mHorizontalPadding;
    }

    public void setmHorizontalPadding(int mHorizontalPadding) {
        this.mHorizontalPadding = mHorizontalPadding;
    }

    public int getoWidth() {
        return oWidth;
    }

    public void setoWidth(int oWidth) {
        this.oWidth = oWidth;
    }

    public int getoHeight() {
        return oHeight;
    }

    public void setoHeight(int oHeight) {
        this.oHeight = oHeight;
    }

    public int getOlpx() {
        return olpx;
    }

    public void setOlpx(int olpx) {
        this.olpx = olpx;
    }

    public int getOtpx() {
        return otpx;
    }

    public void setOtpx(int otpx) {
        this.otpx = otpx;
    }

    public int getmWidth() {
        return mWidth;
    }

    public void setmWidth(int mWidth) {
        this.mWidth = mWidth;
    }

    public int getmHeight() {
        return mHeight;
    }

    public void setmHeight(int mHeight) {
        this.mHeight = mHeight;
    }

    public Bitmap getMaskIn() {
        return maskIn;
    }

    public void setMaskIn(Bitmap maskIn) {
        this.maskIn = maskIn;
    }

}
